﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;

namespace MoLingOverhaul.Content.Projectiles
{
    public class Leaf : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            Main.projFrames[Projectile.type] = 5;
        }

        public override void SetDefaults()
        {
            Projectile.width = 24;
            Projectile.height = 24;
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.penetrate = -1;
            Projectile.timeLeft = 3600;
            Projectile.tileCollide = false;
            Projectile.localNPCHitCooldown = 60;
            Projectile.ignoreWater = true;
            Projectile.alpha = 0;
            Projectile.scale = 1f;           
        }
        public override void AI()
        {
            Projectile.frameCounter++;
            if (Projectile.frameCounter >= 5)
            {
                Projectile.frameCounter = 0;
                Projectile.frame = (Projectile.frame + 1) % 5;
            }
            Projectile.velocity.Y = (float)System.Math.Sin(Main.time * 0.1f + Projectile.ai[0]) * 0.5f;
            Player player = Main.player[Projectile.owner];
            if ((float)player.statLife / player.statLifeMax2 > 0.4f)
            {
                Projectile.Kill();
                return;
            }
            Projectile.Center = player.Center;
            float rotationSpeed = 0.05f;
            float radius = 80f;
            float angle = (float)(Main.time * rotationSpeed + Projectile.ai[0] * MathHelper.TwoPi / 6f);
            Vector2 offset = new Vector2((float)System.Math.Cos(angle), (float)System.Math.Sin(angle)) * radius;
            Projectile.position += offset;
            Projectile.rotation = angle + MathHelper.PiOver2;         
            Lighting.AddLight(Projectile.Center, 0.1f, 0.3f, 0.05f);
        }
        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
        }
        public override bool? CanCutTiles()
        {
            return false;
        }
    }
}
